#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""COG module"""
__author__ = "Lei Chen <hide1713@gmail.com>"
from genome_record import GenomeRecord
import logging

class Cog(object):
    """Represent a COG family
    The class stored the genome record that hits on this COG
    """
    def __init__(self, name, length, category, desc):
        """Constructor
        """
        self._name = name
        self._length = length #the size of COG family
        self._category = category
        self._desc = desc
        self._hits = []
        self._hitp = []
        self._cache = []
        self._file_name = ""

    def _set_hitp(self, value):
        self._hitp = value
        
    def _get_hitp(self):
        return self._hitp
        
    hitp = property(_get_hitp, _set_hitp)    
        
    def _set_file_name(self, value):
        self._file_name = value
        
    def _get_file_name(self):
        return self._file_name
        
    file_name = property(_get_file_name, _set_file_name)
        
    def get_hits_frequency(self, ):
        """Return an array of int that represents the frequency of every
        data point. The size of array is equal to cog length. 
        """
        if len(self._cache) == len(self._hits):
            pass
        else:
            arr = self._length*[0]
            for hit in self._hits:
                # NOTE: range function generates numbers in [start, end)
                for record_range in range(hit.start_pos, hit.end_pos):
                    try:
                        arr[record_range] += 1
                    except IndexError,e:
                        logging.warning("Cog size is %d. However read ends at %d"%(self._length,hit.end_pos))
            self._cache = arr
        return self._cache

    def get_hits_range(self, ):
        """Return a list of genome records' (read, start, end) positions.  
        """
        return [(i.read, i.start_pos, i.end_pos, i.e_val) for i in self._hits]

    def _get_number_of_hits(self):
        return len(self._hits)
    
    number_of_hits = property(_get_number_of_hits)

    def add_hits(self, genome_record):
        """Append genome_record to this cog family
        
        Arguments:
        - `genome_record`: the record to be appended
        """
        assert(genome_record.end_pos <= self._length),(self._name,self._length,genome_record)
        assert(genome_record.start_pos >= 0)
        self._hits.append(genome_record)

    def _get_hits(self):
        return self._hits
    
    hits = property(_get_hits)
            
    def _get_name(self):
        return self._name
    
    name = property(_get_name)
    
    def _get_length(self):
        """The size of COG family"""
        return self._length
    
    length = property(_get_length)
    
    def _get_category(self):
        return self._category
    
    category = property(_get_category)
    
    def _get_desc(self):
        return self._desc
    
    desc = property(_get_desc)
